package client;

import common.Email;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Igor Demidov
 * Date: 25.02.13
 * Time: 15:49
 */
public class MessagesPageGUI {
    private JPanel panel1;
    private JButton inboxButton;
    private JButton outboxButton;
    private JButton sendButton;
    private JButton logoutButton;
    private JList list1;
    private JScrollPane pane;

    public MessagesPageGUI(final MainClient client) {
        final JFrame frame = new JFrame("User: " + client.currentUserLogin);
        frame.setContentPane(this.panel1);
        //final DefaultListModel listModel = new DefaultListModel();
        //list1 = new JList(listModel);
        //frame.setLayout(new BorderLayout());
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        frame.add(pane);
        frame.pack();
        frame.setVisible(true);
        inboxButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    List<Email> emails = client.getInbox();
                    System.err.println(emails);
                    if (emails == null) {
                        //textArea1.setText("Nothing to show");
                        emails = new LinkedList<Email>();
                    }
                    Email[] emailArray = new Email[emails.size()];
                    int ptr = 0;
                    for (Email email: emails) {
                        emailArray[ptr++] = email;
                    }
                    MessageListGUI messageListGUI = new MessageListGUI(emailArray);
                //    list1 = new JList(emailArray);
                  //  list1.setVisibleRowCount(10);
                  //  pane = new JScrollPane(list1);
                   // list1.setVisible(true);
                   // pane.setVisible(true);
                  //  frame.add(pane);
                    //frame.add(pane, BorderLayout.SOUTH);
                    /*
                    listModel.clear();
                    for (Email email: emails) {
                        listModel.addElement(email);
                        list1.ensureIndexIsVisible(listModel.getSize() - 1);
                    }
                    list1.setVisible(true);
                    */
                    //textArea1.setText("Last email from inbox: " + emails.get(emails.size() - 1));
                } catch (IOException e1) {
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (ClassNotFoundException e1) {
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        });
        outboxButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    List<Email> emails = client.getOutbox();
                    System.err.println(emails);
                    /*if (emails == null || emails.isEmpty()) {
                        textArea1.setText("Nothing to show");
                    } else {
                        textArea1.setText("Last email from outbox: " + emails.get(emails.size() - 1));
                    } */
                    if (emails == null) {
                        //textArea1.setText("Nothing to show");
                        emails = new LinkedList<Email>();
                    }
                    Email[] emailArray = new Email[emails.size()];
                    int ptr = 0;
                    for (Email email: emails) {
                        emailArray[ptr++] = email;
                    }
                    MessageListGUI messageListGUI = new MessageListGUI(emailArray);
                    //frame.setLayout(new BorderLayout());
                   // list1 = new JList(emailArray);
                    //list1.setVisibleRowCount(10);
                    //pane = new JScrollPane(list1);
                    //list1.setVisible(true);
                    //pane.setVisible(true);
                    //frame.add(pane);
                    //frame.add(pane, BorderLayout.SOUTH);
                } catch (IOException e1) {
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (ClassNotFoundException e1) {
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        });
        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SendPageGUI sendPageGUI = new SendPageGUI(client);
            }
        });
        logoutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    frame.setVisible(false);
                    client.doLogout();
                } catch (IOException e1) {
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (ClassNotFoundException e1) {
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        });
    }
}
